﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControlSupport_ShoppingCart : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Session["ProIdCard"] == null) || (Session["CountCard"] == null)) return;

        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("imageurl");
        dataTable.Columns.Add("proname");
        dataTable.Columns.Add("size");
        dataTable.Columns.Add("color");
        dataTable.Columns.Add("number");
        dataTable.Columns.Add("price");
        dataTable.Columns.Add("sum");
        dataTable.Columns.Add("edit");
        dataTable.Columns.Add("delete");
        ArrayList ProIdCard = (ArrayList)Session["ProIdCard"];
        ArrayList CountCard = (ArrayList)Session["CountCard"];
        ShoppingDataContext db=new ShoppingDataContext();
        for (int i = 0; i < ProIdCard.Count; i++)
        {
            var product_de = from p in db.PRODUCT_DEs
                             join q in db.PRODUCTs
                             on p.ProductID equals q.ProductID
                             where p.Product_DesID == ProIdCard[i].ToString()
                             select new{p.Product_DesID, p.image, p.price, p.Size, p.Color , q.ProName};

            DataRow row = dataTable.NewRow();
            row["imageurl"] = product_de.First().image;
            row["proname"] = product_de.First().ProName;
            row["price"] = product_de.First().price;
            row["size"] = product_de.First().Size;
            row["color"] = product_de.First().Color;
            row["number"] = CountCard[i].ToString();
            row["sum"] = product_de.First().price * int.Parse(CountCard[i].ToString());
            row["edit"] = "ProductDetails.aspx?id="+product_de.First().Product_DesID;
            row["delete"] = product_de.First().Product_DesID;

            dataTable.Rows.Add(row);
        }
        
        GridView1.DataSource = dataTable;
        GridView1.DataBind();
    }
    protected void DeleteClick(object sender, CommandEventArgs e)
    {
        ArrayList ProIdCard = (ArrayList)Session["ProIdCard"];
        ArrayList CountCard = (ArrayList)Session["CountCard"];
        int index = ProIdCard.IndexOf(e.CommandArgument.ToString());
        CountCard.RemoveAt(index);
        ProIdCard.RemoveAt(index);
        Session["ProIdCard"] = ProIdCard;
        Session["CountCard"] = CountCard;
        Response.Redirect(Request.Url.AbsoluteUri);
    }
}
